﻿using System;
using System.Xml;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.HCLoad.Utils;
using YArchitech.Revit;

namespace YArchitech.HCLoad.Export
{
	public class HcXmlSpace : HcXmlElement
	{
		public HcXmlSpace()
		{
		}

		public HcXmlSpace(HcSpaceData spaceData)
		{
			this.m_spaceData = spaceData;
		}

		public HcSpaceData SpaceData
		{
			get
			{
				return this.m_spaceData;
			}
		}

		public Space Space
		{
			get
			{
				return this.m_spaceData.Space;
			}
		}

		public override bool WriteElementData(ref XmlElement xmlelem_Space)
		{
			Space space = this.m_spaceData.Space;
			if (this.Space == null)
			{
				return false;
			}
			if (!base.WriteElementData(ref xmlelem_Space))
			{
				return false;
			}
			XmlDocument ownerDocument = xmlelem_Space.OwnerDocument;
			xmlelem_Space.SetAttribute("id", space.Id.ToString());
			string name = Enum.GetName(typeof(SpaceType), space.SpaceType);
			xmlelem_Space.SetAttribute("spaceType", name);
			if (space.Zone != null)
			{
				xmlelem_Space.SetAttribute("zoneIdRef", space.Zone.Id.ToString());
			}
			xmlelem_Space.SetAttribute("conditionType", space.ConditionType.ToString());
			xmlelem_Space.SetAttribute("buildingStoreyIdRef", space.LevelId.IntegerValue.ToString());
			this.AddPeopleElement(ref xmlelem_Space);
			this.AddLightElement(ref xmlelem_Space);
			this.AddEquipmentElement(ref xmlelem_Space);
			this.AddAirflowElement(ref xmlelem_Space);
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "Name", space.Name);
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "Area", HcUnit.AreaConverFromAPI(space.Document, space.Area).ToString());
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "Volume", HcUnit.VolumeConverFromAPI(space.Document, space.Volume).ToString());
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "CADObjectId", space.Id.ToString());
			this.AddSurfaces(ref xmlelem_Space);
			return true;
		}

		private bool AddPeopleElement(ref XmlElement xmlelem_Space)
		{
			Space space = this.m_spaceData.Space;
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "PeopleNumber", space.NumberofPeople.ToString()).SetAttribute("unit", "NumberOfPeople");
			XmlElement xmlElement = HcXmlElement.AppendTextElement(ref xmlelem_Space, "PeopleHeatGain", (space.LatentHeatGainperPerson + space.LatentHeatGainperPerson).ToString());
			xmlElement.SetAttribute("unit", "WattPerPerson");
			xmlElement.SetAttribute("heatGainType", Enum.GetName(typeof(LoadType), LoadType.Total));
			XmlElement xmlElement2 = HcXmlElement.AppendTextElement(ref xmlelem_Space, "PeopleHeatGain", space.SensibleHeatGainperPerson.ToString());
			xmlElement2.SetAttribute("unit", "WattPerPerson");
			xmlElement2.SetAttribute("heatGainType", Enum.GetName(typeof(LoadType), LoadType.Sensible));
			XmlElement xmlElement3 = HcXmlElement.AppendTextElement(ref xmlelem_Space, "PeopleHeatGain", space.LatentHeatGainperPerson.ToString());
			xmlElement3.SetAttribute("unit", "WattPerPerson");
			xmlElement3.SetAttribute("heatGainType", Enum.GetName(typeof(LoadType), LoadType.Latent));
			return true;
		}

		private bool AddLightElement(ref XmlElement xmlelem_Space)
		{
			return true;
		}

		private bool AddEquipmentElement(ref XmlElement xmlelem_Space)
		{
			return false;
		}

		private bool AddAirflowElement(ref XmlElement xmlelem_Space)
		{
			XmlDocument ownerDocument = xmlelem_Space.OwnerDocument;
            string formatOptionsName = UnifiedModified.GetFormatOptionsName(this.Space.Document, (Autodesk.Revit.DB.UnitType)15);
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "ActualExhaustAirflow", this.Space.ActualExhaustAirflow).SetAttribute("unit", formatOptionsName);
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "ActualSupplyAirflow", this.Space.ActualSupplyAirflow).SetAttribute("unit", formatOptionsName);
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "ActualReturnAirflow", this.Space.ActualReturnAirflow).SetAttribute("unit", formatOptionsName);
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "DesignExhaustAirflow", this.Space.DesignExhaustAirflow).SetAttribute("unit", formatOptionsName);
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "DesignReturnAirflow", this.Space.DesignReturnAirflow).SetAttribute("unit", formatOptionsName);
			HcXmlElement.AppendTextElement(ref xmlelem_Space, "DesignSupplyAirflow", this.Space.DesignSupplyAirflow).SetAttribute("unit", formatOptionsName);
			return true;
		}

		private bool AddSurfaces(ref XmlElement xmlelem_Space)
		{
			if (this.m_spaceData.SurfaceIds == null)
			{
				return false;
			}
			XmlDocument ownerDocument = xmlelem_Space.OwnerDocument;
			if (this.m_spaceData.SurfaceIds != null)
			{
				foreach (string value in this.m_spaceData.SurfaceIds)
				{
					XmlElement xmlElement = ownerDocument.CreateElement("SpaceBoundary");
					xmlElement.SetAttribute("surfaceIdRef", value);
					xmlelem_Space.AppendChild(xmlElement);
				}
			}
			return true;
		}

		private HcSpaceData m_spaceData;
	}
}
